package d001;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "d001_mytest_a001e", urlPatterns = {"/d001_mytest/a001e"})
public class a001e extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String area_id = request.getParameter("area_id");
        if (area_id == null) {
            area_id = "a1";
        }
        System.out.println("area_id:" + area_id);

        response.setContentType("text/html;charset=UTF-8\n");
        StringBuilder strBldr = new StringBuilder();
        strBldr.append("<!DOCTYPE html>\n");
        strBldr.append("<html>\n");
        strBldr.append("<head>\n");
        strBldr.append("<title>TODO supply a title</title>\n");
        strBldr.append("<meta charset=\"UTF-8\">\n");
        strBldr.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
        strBldr.append("<link href=\"a001.css\" rel=\"stylesheet\" type=\"text/css\" title=\"a001_css\"/>\n");
        strBldr.append("</head>\n");
        strBldr.append("<body>\n");
        strBldr.append("<div class=\"header\"><h1 class=\"header\"> Header2(中文) </h1></div>\n");
        strBldr.append("<div class=\"area\">\n");
        strBldr.append("<h1 class=\"area\">Area (中文) 2</h1>\n");
        strBldr.append("<ul class=\"area\">\n");
        //
        //  --------------------------------------------------------------------
        if ("a1".equalsIgnoreCase(area_id)) {
            strBldr.append(" <li class=\"active_area\">AREA 1 (中文)</li> \n");//<--  記得前後要加空白
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a2'\">AREA 2 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a3'\">AREA 3 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a4'\">AREA 4 (中文)</li> \n");
        } else if ("a2".equalsIgnoreCase(area_id)) {
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a1'\">AREA 1 (中文)</li> \n");
            strBldr.append(" <li class=\"active_area\">AREA 2 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a3'\">AREA 3 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a4'\">AREA 4 (中文)</li> \n");
        } else if ("a3".equalsIgnoreCase(area_id)) {
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a1'\">AREA 1 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a2'\">AREA 2 (中文)</li> \n");
            strBldr.append(" <li class=\"active_area\">AREA 3 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a4'\">AREA 4 (中文)</li> \n");
        } else if ("a4".equalsIgnoreCase(area_id)) {
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a1'\">AREA 1 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a2'\">AREA 2 (中文)</li> \n");
            strBldr.append(" <li class=\"area\"  onclick=\"javascript:location.href='a001e?area_id=a3'\">AREA 4 (中文)</li> \n");
            strBldr.append(" <li class=\"active_area\">AREA 4 (中文)</li> \n");

        }
        //  --------------------------------------------------------------------
        //

        strBldr.append("</ul>\n");
        strBldr.append("</div>\n");
        strBldr.append("<div class=\"area_content\">\n");
        strBldr.append("<div id=\"data_content\">\n");
        strBldr.append("<h1>Test Info</h1>\n");
        strBldr.append("<ul>\n");
        strBldr.append("<li>a</li>\n");
        strBldr.append("<li>b</li>\n");
        strBldr.append("<li>c</li>\n");
        strBldr.append("</ul>\n");
        strBldr.append("</div>\n");
        strBldr.append("</div>\n");
        strBldr.append("<div class=\"footer\">Footer (中文)</div>\n");
        strBldr.append("</body>\n");
        strBldr.append("</html>\n");
        try (PrintWriter out = response.getWriter()) {
            out.println(strBldr.toString());
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
